from tortoise import BaseDBAsyncClient


async def upgrade(db: BaseDBAsyncClient) -> str:
    return """
        CREATE TABLE IF NOT EXISTS `students` (
    `stu_id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
    `username` VARCHAR(50) NOT NULL UNIQUE,
    `password` VARCHAR(100) NOT NULL,
    `head` LONGBLOB,
    `gender` VARCHAR(10),
    `content` LONGTEXT,
    `role` VARCHAR(7) NOT NULL COMMENT 'student: student\nteacher: teacher\nadmin: admin' DEFAULT 'student'
) CHARACTER SET utf8mb4;
CREATE TABLE IF NOT EXISTS `classes` (
    `class_id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
    `class_name` VARCHAR(100) NOT NULL,
    `class_type` VARCHAR(50) NOT NULL,
    `price` DECIMAL(10,2) NOT NULL
) CHARACTER SET utf8mb4;
CREATE TABLE IF NOT EXISTS `files` (
    `file_id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
    `file_name` VARCHAR(255) NOT NULL,
    `file_url` VARCHAR(512) NOT NULL,
    `course_id` INT NOT NULL,
    CONSTRAINT `fk_files_classes_462aed92` FOREIGN KEY (`course_id`) REFERENCES `classes` (`class_id`) ON DELETE CASCADE
) CHARACTER SET utf8mb4;
CREATE TABLE IF NOT EXISTS `contents` (
    `content_id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
    `content_name` VARCHAR(255) NOT NULL,
    `content_url` VARCHAR(512) NOT NULL,
    `course_id` INT NOT NULL,
    CONSTRAINT `fk_contents_classes_2aab76e1` FOREIGN KEY (`course_id`) REFERENCES `classes` (`class_id`) ON DELETE CASCADE
) CHARACTER SET utf8mb4;
CREATE TABLE IF NOT EXISTS `records` (
    `record_id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
    `enroll_time` DATETIME(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
    `status` VARCHAR(20) NOT NULL DEFAULT 'active',
    `course_id` INT NOT NULL,
    `student_id` INT NOT NULL,
    UNIQUE KEY `uid_records_student_d72cf2` (`student_id`, `course_id`),
    CONSTRAINT `fk_records_classes_396ce872` FOREIGN KEY (`course_id`) REFERENCES `classes` (`class_id`) ON DELETE CASCADE,
    CONSTRAINT `fk_records_students_5d2c7e64` FOREIGN KEY (`student_id`) REFERENCES `students` (`stu_id`) ON DELETE CASCADE
) CHARACTER SET utf8mb4;
CREATE TABLE IF NOT EXISTS `history_records` (
    `history_id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
    `history_content_id` INT NOT NULL,
    `student_id` INT NOT NULL,
    CONSTRAINT `fk_history__students_a614bc9e` FOREIGN KEY (`student_id`) REFERENCES `students` (`stu_id`) ON DELETE CASCADE
) CHARACTER SET utf8mb4;
CREATE TABLE IF NOT EXISTS `collections` (
    `collection_id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
    `collection_class_id` INT NOT NULL,
    `collect_time` DATETIME(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
    `tag` VARCHAR(50),
    `student_id` INT NOT NULL,
    UNIQUE KEY `uid_collections_student_7882ae` (`student_id`, `collection_class_id`),
    CONSTRAINT `fk_collecti_students_b93179bc` FOREIGN KEY (`student_id`) REFERENCES `students` (`stu_id`) ON DELETE CASCADE
) CHARACTER SET utf8mb4;
CREATE TABLE IF NOT EXISTS `discussions` (
    `discussion_id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
    `title` VARCHAR(100) NOT NULL,
    `content` LONGTEXT NOT NULL,
    `created_at` DATETIME(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
    `updated_at` DATETIME(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6),
    `status` VARCHAR(6) NOT NULL COMMENT 'ACTIVE: active\nCLOSED: closed\nPINNED: pinned' DEFAULT 'active',
    `discussion_class_id` INT,
    `author_id` INT,
    CONSTRAINT `fk_discussi_students_2e85027b` FOREIGN KEY (`author_id`) REFERENCES `students` (`stu_id`) ON DELETE SET NULL,
    KEY `idx_discussions_discuss_fcc137` (`discussion_class_id`)
) CHARACTER SET utf8mb4;
CREATE TABLE IF NOT EXISTS `comments` (
    `comment_id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
    `content` LONGTEXT NOT NULL,
    `created_at` DATETIME(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
    `top` INT NOT NULL DEFAULT 0,
    `author_id` INT,
    `discussion_id` INT NOT NULL,
    CONSTRAINT `fk_comments_students_3c324bee` FOREIGN KEY (`author_id`) REFERENCES `students` (`stu_id`) ON DELETE SET NULL,
    CONSTRAINT `fk_comments_discussi_eefa7d15` FOREIGN KEY (`discussion_id`) REFERENCES `discussions` (`discussion_id`) ON DELETE CASCADE
) CHARACTER SET utf8mb4;
CREATE TABLE IF NOT EXISTS `products` (
    `product_id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
    `product_class_id` INT NOT NULL,
    `product_class_name` VARCHAR(100) NOT NULL,
    `product_class_price` DECIMAL(10,2) NOT NULL,
    `order_number` VARCHAR(50) NOT NULL UNIQUE,
    `status` VARCHAR(9) NOT NULL COMMENT 'PENDING: pending\nPAID: paid\nCANCELLED: cancelled\nREFUNDED: refunded' DEFAULT 'pending',
    `created_at` DATETIME(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
    `paid_at` DATETIME(6),
    `student_id` INT,
    CONSTRAINT `fk_products_students_c52cf722` FOREIGN KEY (`student_id`) REFERENCES `students` (`stu_id`) ON DELETE SET NULL,
    KEY `idx_products_order_n_527e1f` (`order_number`),
    KEY `idx_products_student_034c0f` (`student_id`)
) CHARACTER SET utf8mb4;
CREATE TABLE IF NOT EXISTS `aerich` (
    `id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
    `version` VARCHAR(255) NOT NULL,
    `app` VARCHAR(100) NOT NULL,
    `content` JSON NOT NULL
) CHARACTER SET utf8mb4;
CREATE TABLE IF NOT EXISTS `models.Record` (
    `class_id` INT NOT NULL,
    `stu_id` INT NOT NULL,
    FOREIGN KEY (`class_id`) REFERENCES `classes` (`class_id`) ON DELETE CASCADE,
    FOREIGN KEY (`stu_id`) REFERENCES `students` (`stu_id`) ON DELETE CASCADE,
    UNIQUE KEY `uidx_models.Reco_class_i_73dcb7` (`class_id`, `stu_id`)
) CHARACTER SET utf8mb4;"""


async def downgrade(db: BaseDBAsyncClient) -> str:
    return """
        """
